package com.eastrobot.doc.poitlconvert.util;

import com.eastrobot.doc.poitlconvert.exception.ExpressionCompileException;
import lombok.extern.slf4j.Slf4j;
import org.mvel2.CompileException;
import org.mvel2.MVEL;

import java.io.Serializable;
import java.util.Map;


@Slf4j
public class MvelUtil {

    public static Object eval(String exp, Map data) {
        log.info("表达式：" + exp);

        String resultStr = "";
        // 类转map
//        Map<String, Object> map = BeanUtil.beanToMap(data);

//        java.util.Map<String, Object> context = new java.util.HashMap<>();
//        context.put("LNGXBGXY01", data.getLNGXBGXY01());
//        context.put("LNGXBGXY02", data.getLNGXBGXY02());
//        context.put("chartDataModels", data.getChartDataModels());
        try {
            Serializable expression1 = MVELCompiler.getInstance().compile(exp);
            Object result = MVEL.executeExpression(expression1, data);
            log.info("解析表达式结果：");
//            resultStr = result.toString();
            return result;
        } catch (CompileException e) {
            log.error(e.getMessage(), e);
            throw new ExpressionCompileException(40001,"表达式错误："+exp);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
//            throw new RuntimeException("其他错误："+exp);
        }

        return "没有获取到数据";
    }
}
